﻿using System;
using System.IO;
using System.Collections.Generic;
using AttachmentModule.Dime;
using System.Xml;
using Coordenadas;
namespace LectorXML
{
    public class XMLLlamadoCarrosSupernova:XMLLlamadoCarros
    {
        private Central2.Emergencia proxy;
        private List<LatLong> carros;

        public XMLLlamadoCarrosSupernova(string url)
        {
            proxy = new Central2.Emergencia(url);
            carros=new List<LatLong>();
        }

        public List<LatLong> Carros()
        {
            /*XmlNode xn;
            try
            {
                xn = this.proxy.Carros();
            }
            catch (Exception)
            {
                try
                {
                    xn = this.proxy.Carros();
                }
                catch (Exception)
                {
                    xn = null;
                    return new List<LatLong>();
                }
            }
            if (xn != null)
            {
                this.carros.Clear();

                XmlNodeList listaCarros = xn.SelectNodes("//carro");
                for (int i = 0; i < listaCarros.Count; i++)
                {
                    if (listaCarros[i].SelectNodes("posicion/lat")[0].InnerText != "" && listaCarros[i].SelectNodes("posicion/lat")[0].InnerText != "")
                    {
                        this.carros.Add(new LatLong(
                            double.Parse(listaCarros[i].SelectNodes("posicion/lat")[0].InnerText),
                            double.Parse(listaCarros[i].SelectNodes("posicion/lon")[0].InnerText),
                            listaCarros[i].SelectNodes("nombre")[0].InnerText
                        ));
                    }
                }
            }
             */
            return this.carros;
        }

        public XmlNodeList listaCarros()
        {
            /*XmlNode xn;
            try
            {
                xn = this.proxy.Carros();
            }
            catch (Exception)
            {
                try
                {
                    xn = this.proxy.Carros();
                }
                catch (Exception)
                {
                    xn = null;
                    return null;
                }
            }
            if (xn != null)
                return xn.SelectNodes("//carro");
             */ 
            return null;
        }


        public XmlNodeList LlamadoCars(string _nroLlamado) 
        {
            XmlNode xn;
            try
            {
                xn = String2XmlNode(this.proxy.LlamadoCarros(_nroLlamado));
            }
            catch (Exception)
            {
                try
                {
                    xn = String2XmlNode(this.proxy.LlamadoCarros(_nroLlamado));
                }
                catch (Exception)
                {
                    xn = null;
                    return null;
                }
            }
            if (xn != null)
                return xn.SelectNodes("//carro");
            return null;

        }

        private XmlNode String2XmlNode(string _texto)
        {
            XmlDocument tmpXmlNode;
            tmpXmlNode = new XmlDocument();
            tmpXmlNode.LoadXml(_texto);
            return tmpXmlNode;
        }

    }

}
